/**   
 * @Title: GetPromoteInfomation.java 
 * @Package com.esun.util.promote 
 * @Description: (获取推广信息) 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-7-28 上午10:28:05  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.util.promote;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.esun.config.Config;
import com.esun.network.XMLReaderAndPaser;
import com.esun.util.log.EDebug;

/**
 * @ClassName: GetPromoteInfomation
 * @Description: (获取推广信息)
 * @author: City Jiangsy@500wan.com
 * @date: 2011-7-28 上午10:28:05
 * 
 */
public class GetPromoteInfomation {
	private static final String EVENT = "event";
	private static final String PRODUCT = "product";
	private Hashtable<String, String> result;
	private static final String HASPROMOTE = "haspromote";
	private static final String ADDRESS = "address";
	private static final String ERROR = "105";
	private static final String POST_END = "post end!!\n============================";
	private static final String STR_DESCRIPTION = "服务器返回数据# \n \n";
	private static final String STR_NOMEAN = "\n \n";
	private static final String STR_HORIZONTAL = "============================";
	private boolean isSucceed;

	public boolean isSucceed() {
		return isSucceed;
	}

	public void setSucceed(boolean isSucceed) {
		this.isSucceed = isSucceed;
	}

	public GetPromoteInfomation() {
		setSucceed(false);
		String strInfo = getMsg();
		if (strInfo == ERROR) {
			setSucceed(true);
			PromoteInfo.getPromoteInfo().setStrPromoteInfo(ERROR);
		} else {
			setSucceed(true);
			XMLReaderAndPaser saxHandler = new XMLReaderAndPaser(strInfo);
			result = new Hashtable<String, String>();
			result = saxHandler.getResult();
			if (Integer.parseInt(result.get(HASPROMOTE)) == 0) {
				EDebug.o(GetPromoteInfomation.this, "no promoteInfo");
				PromoteInfo.getPromoteInfo().setStrPromoteInfo(Config.NUM0);
			} else {
				EDebug.o(GetPromoteInfomation.this, "have promoteInfo");
				PromoteInfo.getPromoteInfo().setStrPromoteInfo(result.get(ADDRESS));
			}
		}
	}

	private String getMsg() {
		HttpPost httpRequest = new HttpPost(Config.PROMOTE_INFO_ADD);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		String strResult = ERROR;
		try {

			params.add(new BasicNameValuePair(EVENT, "3"));

			params.add(new BasicNameValuePair(PRODUCT, "rabbit"));

			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			strResult = EntityUtils.toString(httpResponse.getEntity());

			EDebug.o(POST_END + STR_DESCRIPTION + strResult + STR_NOMEAN + Config.FUNSTRING + STR_HORIZONTAL);

		} catch (Exception e) {
			strResult = ERROR;
		}
		return strResult;
	}

}
